/**
 * 
 */
package kr.co.insoft.publicmesh.controller.template.script;

import kr.co.insoft.core.model.paging.PagingModel;
import kr.co.insoft.core.model.template.script.ScriptEntityModel;
import kr.co.insoft.core.service.template.script.ScriptService;
import kr.co.insoft.framework.exceptions.ServiceException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author IN-SOFT Inc. Park Jong Taek(jtpark@in-soft.co.kr)
 * 
 */
@Controller
@RequestMapping(value = "template/script")
public class ScriptController {

	final String TILES_PAGE_PREFIX = "tiles/template/script";
	final String SINGLE_PAGE_PREFIX = "template/script";

	@Autowired
	ScriptService scriptService;
	
	/**
	 * Script Paging 목록
	 * 
	 * @param model
	 * @param pagingModel
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "lists")
	public String lists(
			Model model,
			@ModelAttribute("pagingModel") PagingModel pagingModel,
			@RequestParam(value = "ip", defaultValue = "N", required = false) String ip)
			throws Exception {
		model.addAttribute("pagingModel", scriptService.getPagingList(pagingModel));
		
		if (ip.equals("Y"))
			return SINGLE_PAGE_PREFIX+"/lists";
		
		return TILES_PAGE_PREFIX + "/lists";
	}

	/**
	 * Script 등록화면
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "createForm", method = RequestMethod.GET)
	public String createForm(
			Model model,
			@RequestParam(value = "ip", defaultValue = "N", required = false) String ip) {
		model.addAttribute(new ScriptEntityModel());
		if (ip.equals("Y"))
			return SINGLE_PAGE_PREFIX+"/createForm";
		
		return TILES_PAGE_PREFIX + "/createForm";
	}

	/**
	 * Script 등록
	 * 
	 * @param scriptEntityModel
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "createForm", method = RequestMethod.POST)
	public String createScript(
			@ModelAttribute("scriptEntityModel") ScriptEntityModel scriptEntityModel,
			@RequestParam(value = "ip", defaultValue = "N", required = false) String ip,
			final BindingResult bindingResult) throws Exception {
		
		if (bindingResult.hasErrors()) {
			if (ip.equals("Y"))
				return SINGLE_PAGE_PREFIX+"/createForm";
			
			return TILES_PAGE_PREFIX + "/createForm";
		}
		
		scriptService.createScript(scriptEntityModel);
		
		if (ip.equals("Y"))
			return "redirect:./lists?ip=Y";
		
		return "redirect:./lists";
	}
	
	/**
	 * Script 상세 화면
	 * 
	 * @param scriptid
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "view", method = RequestMethod.GET)
	public String view(
			Model model,
			@RequestParam("scriptid") String scriptid,
			@RequestParam(value = "ip", defaultValue = "N", required = false) String ip)
			throws Exception {
		model.addAttribute("scriptEntityModel", scriptService.getScript(scriptid));
		if (ip.equals("Y"))
			return SINGLE_PAGE_PREFIX + "/popup";

		return TILES_PAGE_PREFIX + "/view";
	}
	
	/**
	 * Script 수정
	 * 
	 * @param scriptid
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "edit", method = RequestMethod.POST)
	public @ResponseBody ResponseEntity<Boolean> edit(
			@ModelAttribute("scriptEntityModel") ScriptEntityModel scriptEntityModel) {		
		try {
			scriptService.doEditScript(scriptEntityModel);
			return new ResponseEntity<Boolean>(true, HttpStatus.OK);
		} catch (ServiceException e) {
			return new ResponseEntity<Boolean>(false, HttpStatus.OK);
		}
	}
	
	/**
	 * Script 삭제
	 * 
	 * @param ScriptEntityModel
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "remove", method = RequestMethod.POST)
	public @ResponseBody ResponseEntity<Boolean> remove(
			@ModelAttribute("scriptEntityModel") ScriptEntityModel scriptEntityModel) {		
		try {
			scriptService.doRemoveScript(scriptEntityModel);
			return new ResponseEntity<Boolean>(true, HttpStatus.OK);
		} catch (ServiceException e) {
			return new ResponseEntity<Boolean>(false, HttpStatus.OK);
		}
	}
	
	/**
	 * Script Name 중복 체크
	 * 
	 * @param ScriptEntityModel
	 * @return
	 */
	@RequestMapping(value = "isExistScriptName", method = RequestMethod.POST)
	public @ResponseBody ResponseEntity<Boolean> isExistScriptName(
			@ModelAttribute("scriptEntityModel") ScriptEntityModel scriptEntityModel) {
		try {
			if (scriptService.isExistScriptName(scriptEntityModel))
				return new ResponseEntity<Boolean>(true, HttpStatus.OK);
		} catch (ServiceException e) {
			return new ResponseEntity<Boolean>(false, HttpStatus.OK);
		}
		return new ResponseEntity<Boolean>(false, HttpStatus.OK);
	}
}
